home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / memoryread / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  3.9 KB  |  157 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line at half size by converting it to a memory based
  3.         buffer (for demonstrating the use of memory buffers).
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <dos/dos.h>
  9. #include <exec/memory.h>
  10. #include <exec/types.h>
  11.  
  12. #include <clib/dos_protos.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/cybergraphics_protos.h>
  15. #include <clib/intuition_protos.h>
  16.  
  17. #include <pragmas/dos_pragmas.h>
  18. #include <pragmas/exec_pragmas.h>
  19. #include <pragmas/intuition_pragmas.h>
  20. #include <pragmas/cybergraphics_pragmas.h>
  21.  
  22. #include <cybergraphics/cybergraphics.h>
  23.  
  24. #include <imageio/imageio.h>
  25. #include <imageio/imageio_protos.h>
  26. #include <imageio/imageio_pragmas.h>
  27.  
  28. #include <guigfx/guigfx.h>
  29. #include <pragmas/guigfx_pragmas.h>
  30. #include <guigfx/guigfx_protos.h>
  31.  
  32. /* Function prototypes */
  33. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  34.  
  35. extern struct Library *DOSBase;
  36. struct Library *ImageIOBase, *IntuitionBase;
  37.  
  38. void main( int argc, char **argv )
  39. {
  40.     if ( argv[1] != NULL )
  41.     {
  42.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  43.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  44.         if ( IntuitionBase && ImageIOBase )
  45.         {
  46.             struct FileInfoBlock *fib;
  47.  
  48.             fib = AllocDosObject( DOS_FIB, NULL );
  49.             if ( fib )
  50.             {
  51.                 BPTR lock;
  52.  
  53.                 lock = Lock( argv[1], ACCESS_READ );
  54.                 if ( lock )
  55.                 {
  56.                     if ( Examine( lock, fib ) )
  57.                     {
  58.                         UBYTE *rgbbuffer;
  59.  
  60.                         rgbbuffer = AllocVec( fib->fib_Size, MEMF_PUBLIC | MEMF_CLEAR );
  61.                         if ( rgbbuffer )
  62.                         {
  63.                             BPTR fp;
  64.  
  65.                             fp = Open( argv[1], MODE_OLDFILE );
  66.                             if ( fp != NULL )
  67.                             {
  68.                                 if ( Read( fp, rgbbuffer, fib->fib_Size ) == fib->fib_Size )
  69.                                 {
  70.                                     struct ImageHandle *ih;
  71.                                     ULONG err;
  72.  
  73.                                     err = AllocImage( &ih,
  74.                                         IMG_SrcMemStream, rgbbuffer,
  75.                                         IMG_SrcMemStreamSize, fib->fib_Size,
  76.                                         TAG_DONE );
  77.                                     if ( !err )
  78.                                     {
  79.                                         ULONG num = 1, denom = 2;
  80.                                         ULONG x, y, bpp, rs;
  81.                                         UBYTE *buffer, cs, it;
  82.  
  83.                                         err = GetImageAttrs( ih,
  84.                                             IMG_Width, &x,
  85.                                             IMG_Height, &y,
  86.                                             IMG_BytesPerPixel, &bpp,
  87.                                             IMG_ColourSpace, &cs,
  88.                                             IMG_RowSize, &rs,
  89.                                             IMG_ImageType, &it,
  90.                                             IMG_TestScaleNum, num,
  91.                                             IMG_TestScaleDenom, denom,
  92.                                             TAG_DONE );
  93.                                         if ( !err )
  94.                                         {
  95.                                             printf( "width=%ld, height=%ld\n", x, y );
  96.                                             printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  97.                                             printf( "row size=%ld\n", rs );
  98.                                             printf( "image type=%d\n", it );
  99.  
  100.                                             err = ReadImage( ih,
  101.                                                 IMG_ScaleNum, num,
  102.                                                 IMG_ScaleDenom, denom,
  103.                                                 IMG_ImageBuffer, &buffer,
  104.                                                 IMG_ProgressHook, progressFunc,
  105.                                                 TAG_DONE );
  106.                                             if ( !err )
  107.                                             {
  108.                                             }
  109.                                             else printf( "read image error:%d\n", err );
  110.                                         }
  111.                                         else printf( "get image attrs error:%d\n", err );
  112.  
  113.                                         FreeImage( ih );
  114.                                     }
  115.                                     else printf( "alloc image error:%d\n", err );
  116.                                 }
  117.                                 else printf( "read error on file\n" );
  118.  
  119.                                 Close( fp );
  120.                             }
  121.                             else printf( "cant open file\n" );
  122.  
  123.                             FreeVec( rgbbuffer );
  124.                         }
  125.                         else printf( "cant allocate %ld bytes of memory\n", fib->fib_Size );
  126.                     }
  127.                     else printf( "cant examine %s\n", argv[1] );
  128.  
  129.                     UnLock( lock );
  130.                 }
  131.  
  132.                 FreeDosObject( DOS_FIB, fib );
  133.             }
  134.         }
  135.  
  136.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  137.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  138.     }
  139.     else printf( "no file specified\n" );
  140. }
  141.  
  142. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  143. {
  144.     static int prevpercent = 0;
  145.  
  146.     int percent = ( curr * 100 ) / lines;
  147.  
  148.     if ( prevpercent != percent )
  149.     {
  150.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  151.     }
  152.  
  153.     prevpercent = percent;
  154.  
  155.     return NULL;
  156. }
  157.